var api_url = layui.cache.apiPath;
if(!Array.prototype.remove){ Array.prototype.remove = function(val) {
  var index = this.indexOf(val);
  if (index > -1) {
  this.splice(index, 1);
  }
};}
var imgIds = []; // 选中的图片 ids
if(!window.getImg){ window.getImg = function (d){
  var id = $(d).find('img').data('imageid');
  if(!$(d).hasClass("img-selected")){
    if($.inArray(id,imgIds) == -1){
      imgIds.push(id);
    }
  }else{
    if($.inArray(id,imgIds) != -1) {
      imgIds.remove(id);
    }
  }
}}
// 添加远程图片
if(!window.addRemoteImg){ window.addRemoteImg = function (){
  layer.prompt({
    formType: 0,
    value: '',
    title: '请输入图片链接',
    area: '500px',
  }, function(value, index, elem){
    if(value){
      $.ajax({
        type: "post",
        url:  api_url + "api_file/addRemoteImg",
        data: { url:value },
        dataType: "json"
      }).done(function(data) {
        //console.log(data);
        if(data.code == 1){
          imgIds.length = 0;
          window.wxuploadimg.setChecked(0);
          window.wxuploadimg.queryImgList();
        }else{
          layer.msg(data.msg,{ icon:5 });
        }
      });
    }else{
      layer.msg('需要图片链接',{ icon:2 });
    }
  });
  // layer.msg(L('need-sel-img'),{ icon: 2 });
  return false;
}}
// 删除图片
//
if(!window.removeImg){ window.removeImg = function (){
  // imgIds = getImg('.imgs-list>.img-item') || [];
  console.log('del_imgs',imgIds);
  var l = imgIds.length;
  if(l>0){
    var warning = L('tip-del-img',l);
    layer.confirm(warning,{ icon: 3, title:L("tip") },function(index){
      $.ajax({
        type:"POST",
        url: api_url + "api_file/del",
        data:{ imgIds:imgIds },
        context:window,
        dataType: "json",
        success:function(data){
          //console.log(data);
          if(data.code == 1){
            imgIds.length = 0;
            window.wxuploadimg.setChecked(0);
            window.wxuploadimg.queryImgList();
          }else{
            layer.msg(data.msg,{ icon:5 });
          }
        }
      })
      layer.close(index);
    });
  }else{
    layer.msg(L('need-sel-img'),{ icon: 2 });
  }
  return false;
}}
// 模块化 datetimepicker
// layui.use(['jquery','rb','form','datetimepicker','upload','element','wxuploadimg'],function(){
  var layer = layui.layer
  ,form = layui.form
  ,ele = layui.element
  ,rb = layui.rb
  ,$ = layui.$;
  // 图片选择初始化 - 不适用与动态cont
  layui.wxuploadimg.init({
    wr:"#js_pic_select_wrap"
    ,cont:".wxuploaderimg"
    ,query: api_url + "api_file/picturelist?p="
    ,addRemote : api_url + "api_file/addRemote"
  });
  // 表单提交处理图片ids
  $('#body').on('click','.ajax-post',function(){
    var flag = true,img;
    $('.wxuploaderimg').each(function(index, el) {
      $el = $(el);
      img = $el.find('.img-preview img').map(function(i,el) { return el.getAttribute('data-imageid'); }).get().join(',');
      // var multi = $el.data('combine') || false;
      // var ipt   = $el.data('input') || false;
      $el.prev('input').val(img); // 修改图片表单域
    });
    return flag;
  });

  var uploadInst = layui.upload.render({
    elem: '#upload_picture'
    ,url: api_url + "api_file/uploadUserPicture"
    //,accept: 'file' //允许上传的文件类型
    ,multiple: true // ie9+
    ,drag:true
    ,size: 500 //ie9+
    ,accept: 'images' // images、file（所有）、video、audio
    ,field: 'wxshop'
    ,before: function(obj){
      rb.progress('progress-1'); //上传loading
    }
    ,done: function(res, index, upload){
      rb.progress('progress-1',100);
      rb.uploadSuccess(index,res);
    }
    ,error: function(index, upload){
      rb.msg(L('upload-fail'),5);
    }
  });
  // 日期控件初始化
  if($.fn.datetimepicker){
    $("#js_pic_select_wrap input.time").datetimepicker({
      lang: 'ch',
      timepicker: false,
      format: 'Y-m-d',
      allowBlank : true,
    });
    // console.log("日期初始化成功！");
  }
// });